package com.wb3.organizer.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.wb3.organizer.dao.AccountDAO;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Appointment;
import com.wb3.organizer.model.Task;

public class MailService {

	private String mailHost;

	private String mailUsername;

	private String mailPassword;

	private Properties prop;

	private AccountDAO accountDao;

	private AppointmentService appService;

	private TaskService taskService;

	public String getMailHost() {
		return mailHost;
	}

	public void setMailHost(String mailHost) {
		this.mailHost = mailHost;
	}

	public String getMailUsername() {
		return mailUsername;
	}

	public void setMailUsername(String mailUsername) {
		this.mailUsername = mailUsername;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	public Properties getProp() {
		return prop;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public AccountDAO getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}

	public AppointmentService getAppService() {
		return appService;
	}

	public void setAppService(AppointmentService appService) {
		this.appService = appService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public List<Account> getMailAccount() {
		List<Account> accounts = accountDao.getAll();
		List<Account> accountsOut = new ArrayList<Account>();
		for (int i = 0; i < accounts.size(); i++) {
			if (accounts.get(i).getEmail() != null) {
				accountsOut.add(accounts.get(i));
			}
		}
		return accountsOut;
	}

	public List<Appointment> getMailAppointment(List<Account> accounts) {
		List<Appointment> appointmentsOut = new ArrayList<Appointment>();
 		for(int i=0; i<accounts.size(); i++){
 			List<Appointment> appointments = appService.getAppointmentByTime(accounts.get(i), new Date(), "day");
 			appointmentsOut.addAll(appointments);
		}
		return appointmentsOut;
	}

	public List<Task> getMailTask(List<Account> accounts) {
		List<Task> tasksOut = new ArrayList<Task>();
		for(int i=0; i<accounts.size(); i++){
			List<Task> tasks = taskService.getTaskByTime(accounts.get(i), true);
			tasksOut.addAll(tasks);
		}
		return tasksOut;
	}

	public void sendMail(String sendTO, String subject, String text) {

		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

		// 设定mail server
		mailSender.setHost(this.mailHost);

		// 建立邮件消息
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper msgHelper = new MimeMessageHelper(message);

		// 设置邮件样式和内容
		try {
			msgHelper.setFrom(this.mailUsername);
			msgHelper.setTo(sendTO);
			msgHelper.setSubject(subject);
			msgHelper.setText(text);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		// 发送邮件
		mailSender.setUsername(mailUsername);
		mailSender.setPassword(mailPassword);
		mailSender.setJavaMailProperties(prop);
		mailSender.send(message);
	}

}
